package com.oracle.calllog.analyze.reducer;

import java.io.IOException;

import com.oracle.calllog.analyze.model.ContactDimBean;
import com.oracle.calllog.analyze.model.CountDuration;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

public class CallLogAnalyzeReducer extends Reducer<Text, Text, ContactDimBean, CountDuration>{
    private ContactDimBean outKey = new ContactDimBean();
    private CountDuration outValue = new CountDuration();

    //示例接收数据案例
    //张飞,16666666666,2021,-1,-1
    //张飞,16666666666,2021,3,-1
    //张飞,16666666666,2021,4,-1
    //张飞,16666666666,2021,3,12
    //张飞,16666666666,2021,3,14

    //关羽,19999999999,2021,-1,-1
    //关羽,19999999999,2021,3,-1
    //关羽,19999999999,2021,4,-1
    //关羽,19999999999,2021,3,12
    //关羽,19999999999,2021,3,14
    @Override
    protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
        int callSum = 0;
        int callDurationSum = 0;
        for(Text duration : values){
            callSum++;
            callDurationSum += Integer.valueOf(duration.toString());
        }

        String[] keyDatas = key.toString().split("\\|");
        outKey.setCallNo(keyDatas[0]);
        outKey.setCallName(keyDatas[1]);
        outKey.setYearDimension(keyDatas[2]);
        outKey.setMonthDimension(keyDatas[3]);
        outKey.setDayDimension(keyDatas[4]);

        outValue.setCallSum(String.valueOf(callSum));
        outValue.setCallDurationSum(String.valueOf(callDurationSum));

        context.write(outKey, outValue);
    }
}
